'''
Created on Feb 9, 2012

@author: xzhu
'''
import const

class KeyResidues():
    '''
    This Class stores basic information about the key residues, including 
    number, names and the possible charges of them.
    '''

    numberOfkeyResidues       = 0     # number of residues
    namesOfKeyResidues    = []    # residue names
    possibleChargesOfKeyResidues   = []    # a list of all the possible charge states of residues
        
    initCharges = []
    
    def __init__(self):
        '''
        Constructor
        '''
        pass
    
    @classmethod
    def init_from_residue_names(cls, residues):
        '''
        Get the residue head from a list of all residues.
        
        The residues should be in order!!
        '''
        
        
        for res in residues:
            cls.namesOfKeyResidues.append(res)
            
        cls.numberOfkeyResidues = len(cls.namesOfKeyResidues)
        
        for res in cls.namesOfKeyResidues:
            cls.possibleChargesOfKeyResidues.append(const.POSS_IONI[res[:3]])
            
    @classmethod
    def init_from_path_file(cls, fp):
        '''
        Get the information of key residues in the pathway by reading a file.
        
        The residues should be listed in the order of the pathway.
        The file format should be as follows:
        ASPA0085    -    0
        HOHA0402    -    0    +
        HOHA0406    -    0    +
        ARGA0082    0    +
        HOHA0403    -    0    +
        GLUA0194    -    0
        0    0    0    +    0    -
        
        The last line is the charges at initial state.
        The other lines are the possible charges of specific residues.
        The charge of the initial state should follow the same order as the residues are listed. 
        '''
        
        allLines = fp.readlines()
        for line in allLines[:-1]:
            fields = line.split()
            cls.namesOfKeyResidues.append(fields[0])
            crgs = [const.SYMBOLTOINT[eachCrg] for eachCrg in fields[1:]]
            cls.possibleChargesOfKeyResidues.append(crgs)
                
        for eachCharge in allLines[-1].split():
            cls.initCharges.append(const.SYMBOLTOINT[eachCharge])
       
        cls.numberOfkeyResidues = len(cls.namesOfKeyResidues)
        
        # The number of charges and the residues should match.
        if cls.numberOfkeyResidues != len(cls.initCharges):
            import sys
            sys.stderr.write("The number of key residues doesn't match the number of initial charges!\n")
                
            
            